#include <iostream>
#include <unistd.h>
#include <cstring> //C++版本的string.h
#include <cerrno>  //C++版本的errno.h
#include <sys/wait.h>
#include <sys/types.h>
#include <string>

// fork()之后，子进程能拿到父进程的数据

const int size = 1024;

std::string getOtherMessage()
{
    static int cnt = 0;
    std::string messageid = std::to_string(cnt);
    cnt++;
    pid_t self_id = getpid();
    std::string stringid = std::to_string(self_id);

    std::string message = "messageid: ";
    message += messageid;
    message += "my pid is: ";
    message += stringid;
    return message;
}

// 子进程中进行写入
void subProcessWrite(int wfd)
{
    int pipesize = 0;
    std::string message = "father, I am your son!";
    char c = 'A';
    while (true)
    {
        std::cerr << "++++++++++++++++++++++++++" << std::endl;
        std::string info = message + getOtherMessage();//这条消息就是子进程发给父进程的消息
        //在写入管道时，如果要写入的是一个字符序列，没有必要在最后再添一个 \0作为结束
        write(wfd, info.c_str(), info.size());//
        std::cerr << info << std::endl;

        // sleep(1);//子进程写慢一点
        // write(wfd, &c, 1);
        // std::cout << "pipesize: " << ++pipesize << "write charactor is: " << c << std::endl;
        // c++;
        // if (c == 'G')
        // {
        //     break;
        // }
        // sleep(1);
    }
    std::cout << "child quit ..." << std::endl;
}

void fatherProcessRead(int rfd)
{
    char inbuffer[size];
    while (true)
    {
        sleep(2);
        std::cout << "-------------------------------" << std::endl; 
        // sleep(500);
        // ssize_t 实际上就是整型 int
        ssize_t n = read(rfd, inbuffer, sizeof(inbuffer) - 1);
        if (n > 0)
        {
            inbuffer[n] = 0;
            std::cout << "father get message: " << inbuffer << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "father get return val: " << n << "father quit too!" << std::endl;
            break;
        }
        else
        {
            std::cerr << "read error" << std::endl;
            break;
        }

        // sleep(1);
        // break;
    }
}

int main()
{
    // 1. 创建管道
    int pipefd[2];
    int n = pipe(pipefd);
    if (n != 0)
    {
        std::cerr << "errno:" << errno << ":" << "errstring : " << strerror(errno) << std::endl;
        return 1;
    }

    //  pipe这个函数将会利用pipefd这个输出型参数，
    //  申请成功之后会往pipefd这个整型数组中放入两个文件标识符
    //  比较小的那个对应的是  以读方式打开的文件file
    //  比较大的那个对应的是  以写方式打开的文件file
    //  pipefd[0]->0->r  pipefd[1]->1->w
    std::cout << "pipe[0]: " << pipefd[0] << ", pipefd[1]: " << pipefd[1] << std::endl;
    sleep(1);

    // 2. 创建子进程
    pid_t id = fork();
    if (id == 0)
    {
        std::cout << "子进程关闭不需要的fd了, 准备发消息了" << std::endl;
        sleep(1);
        // 子进程 -- write
        // 3. 关闭不需要的描述符fd，由于是在子进程中写，那么把读对应的文件描述符关掉
        close(pipefd[0]);
        subProcessWrite(pipefd[1]);
        close(pipefd[1]);
        exit(0); // 子进程逻辑运行完成直接结束进程
    }

    std::cout << "父进程关闭不需要 fd了, 准备收消息了" << std::endl;
    sleep(1);
    // 父进程 -- read
    // 3. 关闭不需要的描述符fd
    close(pipefd[1]);
    fatherProcessRead(pipefd[0]);
    std::cout << "5s, father 关闭了自己的读进程" << std::endl;
    sleep(5);
    close(pipefd[0]);

    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
        std::cout << "wait child process done, exit signal: " << (status & 0x7f) << std::endl;
        std::cout << "wait child process done, exit code: " << ((status >> 8) & 0xff) << std::endl;
    }

    return 0;
}
